package com.cowin.base.auth.jwt;

import com.cowin.base.exception.ServiceException;
import com.cowin.sys.model.TmSysUser;
import com.cowin.sys.service.TmSysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C), 1998-2018, 奇瑞信息
 *
 * @author : 于超
 * @Date: 2019/9/18 10:38
 * @Description:
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private TmSysUserService tmSysUserService;

    /**
     * 提供一种从用户名可以查到用户并返回的方法【本系统使用手机号account进行唯一用户验证】
     *
     * @param account
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        TmSysUser tmSysUser = tmSysUserService.findUserByUserName(account);
        User user = new User();
        user.setAccount(account);
        user.setPwd(tmSysUser.getPassword());
        user.setUserId(tmSysUser.getTmSysUserId());
        List<String> roles = new ArrayList<>();
        roles.add("ROLE_ADMIN_TEST");
        user.setRoles(roles);
        return JwtUserFactory.create(user);
    }
}
